package com.findme.unvelouse;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.findme.unvelouse.adapter.FavoriAdapter;
import com.findme.unvelouse.bo.Favori;
import com.findme.unvelouse.bo.VeloStation;
import com.findme.unvelouse.facade.VeloStationFacade;

public class FavoriActivity extends ListActivity {

	private static final int MENU_HELP = Menu.FIRST;

	private static final int DIALOG_HELP = 0;

	private ProgressDialog progressDialog = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favori);

		VeloStationFacade facade = new VeloStationFacade();

		List<VeloStation> veloStationFavoris = facade.getVeloStationFavoris();

		progressDialog = ProgressDialog.show(FavoriActivity.this, "", "Calcul des emplacements disponibles ...", true, true);

		LoadFavoriTask task = new LoadFavoriTask();
		task.execute(veloStationFavoris);

		// List<Favori> mFavoriList = new ArrayList<Favori>();
		//
		// for (VeloStation v : veloStationFavoris) {
		// facade.updateAvailability(v);
		// mFavoriList.add(new Favori(v.getNoStation(), v.getName(),
		// v.getNbTotalBorne(), v.getNbBikeAvailable()));
		// }
		//
		// FavoriAdapter adapter = new FavoriAdapter(mFavoriList, this);
		//
		// setListAdapter(adapter);

	}

	private class LoadFavoriTask extends AsyncTask<List<VeloStation>, Integer, List<Favori>> {

		@Override
		protected List<Favori> doInBackground(List<VeloStation>... value) {

			List<Favori> mFavoriList = new ArrayList<Favori>();

			VeloStationFacade facade = new VeloStationFacade();

			for (VeloStation v : value[0]) {
				facade.updateAvailability(v);
				mFavoriList.add(new Favori(v.getNoStation(), v.getName(), v.getNbTotalBorne(), v.getNbBikeAvailable()));
			}

			return mFavoriList;
		}

		@Override
		protected void onProgressUpdate(Integer... progress) {
			// setProgressPercent(progress[0]);
		}

		@Override
		protected void onPostExecute(List<Favori> result) {

			if (progressDialog != null) {
				progressDialog.cancel();
				progressDialog = null;
			}

			FavoriAdapter adapter = new FavoriAdapter(result, FavoriActivity.this);

			setListAdapter(adapter);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_HELP, 0, R.string.Help);
		MenuItem item = menu.getItem(0);
		item.setIcon(android.R.drawable.ic_menu_info_details);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case MENU_HELP:
			showDialog(DIALOG_HELP);
		}

		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		switch (id) {
		case DIALOG_HELP:
			dialog = new Dialog(this);

			// dialog.setContentView(android.R.layout);
			dialog.setTitle(R.string.Help);
			TextView tv = new TextView(this);
			tv.setPadding(5, 0, 0, 5);
			tv.setText(R.string.HelpFavoriContent);
			dialog.setContentView(tv);

			break;
		default:
			dialog = null;
		}
		return dialog;
	}

}